/**
 * 申请信息列表页表格封装
 * @returns
 */
function getListCols() {
    return [
        {type: "checkbox", fixed: "left"},
        {
            field: "applyUserinfo", minWidth: '150', title: "申请人", templet: function (d) {
                return d?.applyUserinfo?.nickname + '-[' + d.applyUserinfo.department.name + ']'
            }
        },
        {field: "applyTime", title: "申请时间", minWidth: '160'},
        {
            field: "applyCar", maxWidth: '100', title: "申请车辆", templet: function (d) {
                return d?.applyCar?.carNo ?? ''
            }
        },
        {
            field: "car", maxWidth: '100', title: "实际用车", templet: function (d) {
                return d?.car?.carNo ?? ''
            }
        },
        {
            field: "applyType", maxWidth: '100', title: "驾驶类型", templet: function (d) {
                if (d.applyType === 1) {
                    return '自行驾驶';
                } else {
                    return '指派司机';
                }
            }
        },
        {
            field: "applyDriver", title: "申请司机", templet: function (d) {
                return d?.applyDriver?.nickname ?? ''
            }
        },
        {
            field: "driver", title: "驾驶司机", templet: function (d) {
                return d?.driver?.nickname ?? ''
            }
        },
        {field: "applyStartTime", title: "开始时间", minWidth: '160'},
        {field: "applyEndTime", title: "结束时间", minWidth: '160'},
        {
            field: "applyStatus",
            fixed: "right",
            title: "流程状态",
            maxWidth: '100',
            templet: function (d) {
                return getApplyStatusText(d.applyStatus)
            }
        },
        {title: "操作", templet: "#optTemplet", fixed: "right", minWidth: '140'}
    ]
}

/**
 * 申请状态格式化
 * @param applyStatus
 * @returns {string}
 */
function getApplyStatusText(applyStatus) {
    switch (applyStatus) {
        case 1:
            return "已申请";
        case 2:
            return "部门同意";
        case 3:
            return "部门拒绝";
        case 4:
            return "管理员同意";
        case 5:
            return "管理员拒绝";
        case 6:
            return "已还车";
        default:
            return "流程异常！";
    }
}

/**
 * 期日选择器的侧边按钮
 * @returns
 */
function getDateShortcuts() {
    return [
        {
            text: "昨天",
            value: function () {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth();
                const d = date.getDate();

                return [
                    new Date(year, month, d - 1),
                    new Date(year, month, d - 1, 23, 59, 59)
                ]
            }
        }, {
            text: "今天",
            value: function () {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth();
                const d = date.getDate();

                return [
                    new Date(year, month, d),
                    new Date(year, month, d, 23, 59, 59)
                ]
            }
        },
        {
            text: "明天",
            value: function () {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth();
                const d = date.getDate();

                return [
                    new Date(year, month, d + 1),
                    new Date(year, month, d + 1, 23, 59, 59)
                ]
            }
        },
        {
            text: "最近一周",
            value: function () {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth();
                const d = date.getDate();

                return [
                    new Date(year, month, d - 7),
                    new Date(year, month, d, 23, 59, 59)
                ]
            }
        },
        {
            text: "上个月",
            value: function () {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth();
                return [
                    new Date(year, month - 1, 1),
                    new Date(year, month, 0, 23, 59, 59)
                ];
            }
        },
        {
            text: "这个月",
            value: function () {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth();
                return [
                    new Date(year, month, 1),
                    new Date(year, month + 1, 0, 23, 59, 59)
                ];
            }
        },
        {
            text: "下个月",
            value: function () {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth();
                return [
                    new Date(year, month + 1, 1),
                    new Date(year, month + 2, 0, 23, 59, 59)
                ];
            }
        }
    ]
}

/**
 * 封装车辆下拉框
 */
function getCarSelect(id = "applyCarId") {
    $.ajax({
        url: "car/select",
        method: "get",
        dataType: "json",
        success(res) {
            if (res.code === 200) {
                let $html = '<option value="">[指派车辆]</option>';
                for (const car of res.data) {
                    let disabled = '';
                    if (car.status !== 1) {
                        disabled = 'disabled';
                    }
                    $html += '<option ' + disabled + ' value="' + car.carId + '">' + car.carNo + '</option>';
                }
                $("#" + id).empty().append($html);
                form.render();
            }
        }
    })
}

function getDriverSelect(id = "applyDriverId") {
    $.ajax({
        url: "userinfo/driver/select",
        method: "get",
        dataType: "json",
        success(res) {
            if (res.code === 200) {
                let $html = '<option value="">[指派司机]</option>';
                for (const user of res.data) {
                    let disabled = '';
                    if (user.status !== 1) {
                        disabled = 'disabled';
                    }
                    $html += '<option ' + disabled + ' value="' + user.userinfoId + '">' + user.nickname + '</option>';
                }
                $("#" + id).empty().append($html);
                form.render();
            }
        }
    })
}
